package com.weiz.thread.scheduledexecutor;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

// 自定义线程工厂，用于给线程命名
public class NamedThreadFactory implements ThreadFactory {
    private final String namePrefix;
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    public NamedThreadFactory(String namePrefix) {
        this.namePrefix = namePrefix;
    }
    @Override
    public Thread newThread(Runnable r) {
        return new Thread(r, namePrefix + "-" + threadNumber.getAndIncrement());
    }
}